/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.dept.controller.assembler;

import cn.hutool.core.date.DatePattern;
import com.google.common.collect.Lists;
import net.siufung.core.utils.CopyUtil;
import net.siufung.core.utils.DateTimeUtil;
import net.siufung.core.utils.StringPool;
import net.siufung.market.base.dept.controller.model.vo.DeptExcelVo;
import net.siufung.market.base.dept.controller.model.vo.DeptVo;
import net.siufung.market.base.dept.enums.DeptTypes;
import net.siufung.market.base.dept.mapper.entity.Dept;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 *
 * 系统部门 持久层展示层组装器
 *
 * @author 陈建峰
 * @since 2022-07-13 22:40:52
 */
public class DeptAssembler {

    /**
     * Dept 转换成 DeptVo
     * @param dept 数据库实体
     * @return DeptVo 视图
     */
    public static DeptVo from(Dept dept){
        if(!Objects.isNull(dept)){
            DeptVo sectorVo = new DeptVo();
            CopyUtil.copyProperties(dept, sectorVo);
            return sectorVo;
        }
        return null;
    }

    /**
     * deptList 转换成 DeptVoList
     * @param deptList 数据库实体集合
     * @return List<DeptVo> 视图集合
     */
    public static List<DeptVo> fromList(List<Dept> deptList){
        List<DeptVo> deptVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(deptList)){
            DeptVo deptVo;
            for (Dept dept: deptList) {
                deptVo = new DeptVo();
                CopyUtil.copyProperties(dept, deptVo);
                deptVoList.add(deptVo);
            }
        }
        return deptVoList;
    }

    /**
     * SectorList 转换成 SectorVoList
     * @param deptList 数据库实体集合
     * @param parentDeptList 数据库实体集合
     * @return List<DeptVo> 视图集合
     */
    public static List<DeptVo> fromList(List<Dept> deptList, List<Dept> parentDeptList){
        List<DeptVo> deptVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(deptList)){
            DeptVo deptVo;
            for (Dept dept: deptList) {
                deptVo = new DeptVo();
                CopyUtil.copyProperties(dept, deptVo);
                for (Dept parentDept: parentDeptList) {
                    if(dept.getParentId().equals(parentDept.getId())){
                        deptVo.setDeptParentName(parentDept.getDeptName());
                    }
                }
                if(StringPool.ZERO.equals(dept.getParentId())){
                    deptVo.setDeptParentName("-");
                }
                deptVoList.add(deptVo);
            }
        }
        return deptVoList;
    }

    /**
     * deptList 转换成 DeptExcelVoList
     * @param deptList 数据库实体集合
     * @return List<DeptVo> 视图集合
     */
    public static List<DeptExcelVo> fromListForExcel(List<Dept> deptList){
        List<DeptExcelVo> deptExcelVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(deptList)){
            DeptExcelVo deptVo;
            for (Dept dept: deptList) {
                deptVo = new DeptExcelVo();
                CopyUtil.copyProperties(dept, deptVo);
                deptVo.setDeptType(DeptTypes.getDesc(deptVo.getDeptType()));
                deptVo.setCreatedAt(DateTimeUtil.format(dept.getCreatedAt(), DatePattern.NORM_DATETIME_FORMATTER));
                deptExcelVoList.add(deptVo);
            }
        }
        return deptExcelVoList;
    }
}

